package org.elanfox.intellicommunity.intellicommunityadmin.service.impl;

import org.elanfox.intellicommunity.biz.service.core.equipment.BaseStationService;
import org.elanfox.intellicommunity.common.constant.RedisConstant;
import org.elanfox.intellicommunity.intellicommunityadmin.service.CmdReceiveService;
import org.elanfox.intellicommunity.msg.dto.*;
import org.elanfox.intellicommunity.msg.ienum.CmdEnums;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/3/20
 */
@Service
public class CmdReceiveServiceImpl implements CmdReceiveService {
    @Autowired
    private BaseStationService baseStationService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

//    @Override
//    public void lfScope(CmdDTO cmdDTO) {
//        RawSetResultDTO setResultDTO = (RawSetResultDTO) cmdDTO.getRawMsgDTO();
//        if (setResultDTO.getSetResult() == CmdEnums.SetResult.SUCCESS) {
//            //修改
//            BaseStationPO baseStationPO = baseStationService.findByDeviceAddr(cmdDTO.getCmdWriteParam().getDeviceAddr());
//            CmdLfScopeWriteParam lfScopeWriteParam = (CmdLfScopeWriteParam) cmdDTO.getCmdWriteParam();
//            baseStationPO.setLfScope(lfScopeWriteParam.getScope());
//            baseStationService.save(baseStationPO);
//
//            cmdDTO.getCmdStatus().setStatus(CmdEnums.Status.HANDLE_SUCCESS);
//        } else {
//            cmdDTO.getCmdStatus().setStatus(CmdEnums.Status.FAILED);
//            cmdDTO.getCmdStatus().setMsg(setResultDTO.getSetResult().getDesc());
//        }
//        redisTemplate.opsForHash().put(
//                RedisConstant.getKeyCmd(cmdDTO.getHexAddr()),
//                cmdDTO.getCmdCode().getCode(),
//                cmdDTO
//        );
//    }
//
//    @Override
//    public void rssiFilter(CmdDTO cmdDTO) {
//        RawSetResultDTO setResultDTO = (RawSetResultDTO) cmdDTO.getRawMsgDTO();
//        if (setResultDTO.getSetResult() == CmdEnums.SetResult.SUCCESS) {
//
//        }
//    }

    @Override
    public void receive(CmdDTO cmdDTO) {
        RawSetResultDTO setResultDTO = (RawSetResultDTO) cmdDTO.getRawMsgDTO();
        if (setResultDTO.getSetResult() == CmdEnums.SetResult.SUCCESS) {
            try {
                switch (cmdDTO.getCmdCode()) {
                    case LF_SCOPE:
                        CmdLfScopeWriteParam writeParam = (CmdLfScopeWriteParam) cmdDTO.getCmdWriteParam();
                        baseStationService.setLfScope(cmdDTO.getStationId(), writeParam.getScope());
                        break;
                    case RSSI_FILTER:
                        CmdRssiFilterWriteParam filterWriteParam = (CmdRssiFilterWriteParam) cmdDTO.getCmdWriteParam();
                        baseStationService.setRssiLimit(cmdDTO.getStationId(), filterWriteParam.isEnabled(), filterWriteParam.getLimitVal());
                        break;
                    case BUZZER_STATUS:
                        CmdBuzzerStatusWriteParam buzzerStatusWriteParam = (CmdBuzzerStatusWriteParam) cmdDTO.getCmdWriteParam();
                        baseStationService.setBuzzerStatus(cmdDTO.getStationId(), buzzerStatusWriteParam.isEnabled());
                        break;
                    case DEVICE_ADDR:
                        CmdAddrWriteParam addrWriteParam = (CmdAddrWriteParam) cmdDTO.getCmdWriteParam();
                        baseStationService.setDeviceAddr(cmdDTO.getStationId(), addrWriteParam.getNo());
                        break;
                    case FILTER_WAITING_TIME:
                        CmdFilterWriteParam param = (CmdFilterWriteParam) cmdDTO.getCmdWriteParam();
                        baseStationService.setFilterTime(cmdDTO.getStationId(), param.getInterval());
                        break;
                    default:
                        break;
                }

                cmdDTO.getCmdStatus().setStatus(CmdEnums.Status.HANDLE_SUCCESS);
            } catch (Exception ex) {
                cmdDTO.getCmdStatus().setStatus(CmdEnums.Status.FAILED);
                cmdDTO.getCmdStatus().setMsg(ex.getMessage());
            }
        } else {
            cmdDTO.getCmdStatus().setStatus(CmdEnums.Status.FAILED);
            cmdDTO.getCmdStatus().setMsg(setResultDTO.getSetResult().getDesc());
        }
        redisTemplate.opsForHash().put(
                RedisConstant.getKeyCmd(cmdDTO.getHexAddr()),
                cmdDTO.getCmdCode().getCode(),
                cmdDTO
        );
    }
}
